package com.homegoods.orderinfodetail.presenter;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.aroutercomment.UserService;
import com.homegoods.classify.model.Constant;
import com.homegoods.orderinfodetail.OrderInfoApiUtils;
import com.homegoods.orderinfodetail.model.AddCollectParams;
import com.homegoods.orderinfodetail.model.BannerGoodsBean;
import com.homegoods.orderinfodetail.model.OrderInfoDetailModel;
import com.homegoods.orderinfodetail.model.PicViewInfo;
import com.homegoods.orderinfodetail.ui.view.IDetailView;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.ToastUtil;
import com.previewlibrary.GPreviewBuilder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * @author QiQi
 */
public class OrderInfoDetailPresenter extends BasePresenter<IDetailView> {
    private List<PicViewInfo> mPicInfo;

    public OrderInfoDetailPresenter(IDetailView view) {
        super(view);
    }

    /**
     * 商品评论列表
     */
    public void showCommentList() {
        ARouter.getInstance()
                .build(ARouterPath.PATH_GOODS_COMMENT_LIST)
//                .withString("id", )  //商品ID
                .navigation();
    }

//    public void loadData(String sid) {
//        OrderInfoApiUtils.getCommodityDetails(sid, new Consumer<OrderInfoDetailModel>() {
//            @Override
//            public void accept(OrderInfoDetailModel model) throws Exception {
//                List<BannerGoodsBean> bannerList = new ArrayList<>();
//                mPicInfo = new ArrayList<>();
//                for (OrderInfoDetailModel.ImgBean s : model.getImgList()) {
//                    BannerGoodsBean goodsBean = new BannerGoodsBean();
//                    goodsBean.setIsVideo(1);
//                    goodsBean.setUrl(Constant.SERVER_URL + s.getUrl());
//                    bannerList.add(goodsBean);
//                    //add shibo.zheng
//                    //定义一个集合，将Url设置到PicViewInfo对象中
//                    PicViewInfo picViewInfo = new PicViewInfo();
//                    picViewInfo.setImgUrl(goodsBean.getUrl());
//                    mPicInfo.add(picViewInfo);
//                }
//                mView.setBanner(bannerList);
//                mView.setGoods(model);
//            }
//        }, throwable -> {
//
//        });
//    }

    public void loadData(String sid) {
        OrderInfoApiUtils.getCommodityDetails(sid, model -> {
            Log.d("zhengshibo", "loadData: ============== 成功了 " + model.toString());
            List<BannerGoodsBean> bannerList = new ArrayList<>();
            mPicInfo = new ArrayList<>();
            String[] albumPics = model.getAlbumPics();
            for (String s : albumPics) {
                BannerGoodsBean bean = new BannerGoodsBean();
                bean.setIsVideo(1);
                bannerList.add(bean);
                bean.setUrl(HomeGoodsApp.getOSSHost() + "/" + s);
                //定义一个集合，将Url设置到PicViewInfo对象中
                PicViewInfo picViewInfo = new PicViewInfo();
                picViewInfo.setImgUrl(bean.getUrl());
                mPicInfo.add(picViewInfo);
            }
            Log.d("zhengshibo", "loadData: bannerList size == " + bannerList.size());
            mView.setBanner(bannerList);
            mView.setGoods(model);
            mView.setSKUList(model);
        }, throwable -> {
            Log.d("zhengshibo", "loadData: ====== 失败了");
        });
    }

    /**
     * 获取店铺详情商品规格
     *
     * @param sid
     * @return
     */
    public Observable<String> getSpecifications(String sid) {
        OrderInfoDetailModel model = new OrderInfoDetailModel();
        List<OrderInfoDetailModel.SkuListBean> skuList = model.getSkuList();
        return OrderInfoApiUtils.getCommoditySpecifications(sid);
    }

    /**
     * 获取店铺详情的商品参数
     *
     * @param sid
     * @return
     */
    public Observable<String> getParameter(String sid) {
        return OrderInfoApiUtils.getCommodityParameter(sid);
    }

    /**
     * 店铺详情
     */
    public void showShop(String shopSid) {
        ARouter.getInstance()
                .build(ARouterPath.PATH_SHOP_DETAIL)
                .withString("shopSid", shopSid) //店铺ID
                .navigation();
    }

    /**
     * 立即购买
     */
    public void buyImmediately() {
        //从商品详情界面点击立即购买，跳转到商品详情界面
        ARouter.getInstance()
                .build(ARouterPath.PATH_CONFIRM_ORDER)
//                .withString("id", sid)  //商品ID
                .navigation();
    }

    /**
     * 购物车
     */
    public void showShoppingCar() {
        ARouter.getInstance()
                .build(ARouterPath.PATH_SHOPPING_CART)
                .navigation();
    }


    /**
     * 店铺客服
     */
//    public void showService() {
//        ARouter.getInstance()
//                .build(ARouterPath.PATH_GOODS_SERVICE)
//                .navigation();
//    }

    /**
     * 商品收藏
     */
    public void addCollect(String sid) {
        AddCollectParams params = new AddCollectParams();
        params.setProductId(sid);
        OrderInfoApiUtils.addVideoCollect(params, jsonObject -> {
            ToastUtil.showToast("收藏成功");
        }, throwable -> {
        });
    }

    //add shibo.zheng
    /**
     * 商品详情界面点击图片后预览
     * @param position 当前图片的position
     */
    public void bannerItemClick(int position){
        GPreviewBuilder.from(mView.getContext())
                .setCurrentIndex(position)
                .setData(mPicInfo)  //设置图片集合数据
                .setType(GPreviewBuilder.IndicatorType.Number)
                .start();

    }

}